import config from './config.js'
/*基于uni-app toast showModal 封装*/
//简单的提醒功能 toast
function showToast(title, type = 'default') {
    if (title.length > 15) {
        console.error('toast长度超过15个字符,当前长度为' + title.length)
        return
    }
    var icon = 'none'
    if (type) {
        switch (type) {
            case 'default':
                icon = 'none'
                break;
            case 'success':
                icon = 'success'
                break;
            case 'err':
                icon = 'error'
                break;

        }
    }
    uni.showToast({
        title,
        icon
    })
}

const toast = {
     success:function (title){
         showToast(title,'success')
     },
     error:function (title){
         showToast(title,'err')
     },
     info:function (title){
         showToast(title)
     },
 }

//需要操作的 确认 取消等
function confirm(title, confirmColor) {
    return new Promise((res, rej) => {
        uni.showModal({
            title,
            // cancelColor: '#b6b6b6',
            confirmColor: confirmColor || config.modalColor,
            success: (result) => {
                if (result.cancel) {
                    console.log('取消')
                   rej(result)
                } else  {
                    res(result)
                }
            }

        })
    })
}
//无需操作的confirm 可以用作提醒功能
function message(content, confrimText) {
    return new Promise((res) => {
        uni.showModal({
            title: '提示',
            content,
            showCancel: false,
            confirmColor: config.modalColor,
            success: (result) => {
                res(result)
            }
        })
    })
}

module.exports = {toast,confirm,message}

